package com.xm.xmapp.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;

import com.xm.xmapp.R;
import com.xm.xmapp.http.ThreadPoolFactory;

public class CircleTextView extends TextView {

	public CircleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

	}

	public CircleTextView(Context context) {
		super(context);
		init(context);
	}

	public CircleTextView(Context context, String text) {
		this(context);
		this.text = text;
	}

	private void init(Context context) {
		circle_width = (int) getResources().getDimension(R.dimen.dp2);
		paint.setStrokeWidth(circle_width);
		paint.setStyle(Paint.Style.STROKE);
		// paint.setShadowLayer(30, 5, 2, Color.GREEN);
		paint.setColor(0XFFF2CC1C);
		textSize = (int) getResources().getDimension(R.dimen.dp44);
		text = "           ";
		_paint_text.setTextSize(textSize);
		_paint_text.setColor(Color.BLACK);
		// _paint_text.setShadowLayer(30, 5, 2, Color.GREEN);
		_paint_text.setTypeface(Typeface.DEFAULT);
		_paint_text.setTextAlign(Align.CENTER);

		textHeight = (int) -(_paint_text.ascent() + _paint_text.descent());
	}

	Paint paint = new Paint();
	String text;
	Paint _paint_text = new Paint();

	float circle_width;
	int textHeight;
	float textSize;

	public CircleTextView(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.CircleTextView);

		circle_width = a.getDimension(R.styleable.CircleTextView_circle_width,
				20);

		int circle_color = a.getColor(R.styleable.CircleTextView_circle_color,
				0XFF00FF00);

		paint.setStrokeWidth(circle_width);
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(circle_color);
		// _paint_text.setShadowLayer(30, 5, 2, Color.GREEN);
		textSize = a.getDimension(R.styleable.CircleTextView_circle_textSize,
				32);
		text = (String) a.getText(R.styleable.CircleTextView_circle_text);

		if (text == null) {
			text = "           ";
		}
		if (textSize != 0) {
			_paint_text.setTextSize(textSize);
		}

		_paint_text.setColor(Color.BLACK);
		// _paint_text.setShadowLayer(30, 5, 2, Color.GREEN);
		_paint_text.setTypeface(Typeface.DEFAULT);
		_paint_text.setTextAlign(Align.CENTER);

		textHeight = (int) -(_paint_text.ascent() + _paint_text.descent());

		a.recycle();

	}

	float startAngle01 = 0;
	float sweepAngle01 = 0;

	public void setText(String text) {
		this.text = text;

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		_paint_text.setTextSize(textSize);
		int lingWith = (int) _paint_text.measureText("10000.00");
//		lingWith =(int) getResources().getDimension(R.dimen.dp183);
		int lingWith1 = (int) getResources().getDimension(R.dimen.dp40);
		widthSize = (int) (lingWith + lingWith1);

		setMeasuredDimension(widthSize, widthSize);

	}

	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);

		RectF rect = new RectF(circle_width / 2, circle_width / 2, getWidth()
				- circle_width / 2, getHeight() - circle_width / 2);
		canvas.drawArc(rect, startAngle01, sweepAngle01, false, paint);

//		if (isFirst) {
//			isFirst = false;
			_paint_text.setTextSize(textSize);
			canvas.drawText(text, getWidth() / 2, getHeight() / 2, _paint_text);// 绘制上去字，开始未知x,y采用那只笔绘制

			int textSize2 = (int) getResources().getDimension(R.dimen.dp15);
			_paint_text.setTextSize(textSize2);
			canvas.drawText("实付金额", getWidth() / 2, getHeight() / 2
					+ textHeight, _paint_text);// 绘制上去字，开始未知x,y采用那只笔绘制
//		}

	}

//	boolean isFirst = true;
	private Handler circleHandler = new Handler() {

		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			sweepAngle01++;
			invalidate();

		}
	};

	public void startCanvas() {
		sweepAngle01 = 0;
		ThreadPoolFactory.getInstance().execute(new Runnable() {

			@Override
			public void run() {
				while (sweepAngle01 <= 360) {
					try {
						Thread.sleep(4);
						/**
						 * 在新的线程中，发送消息给View，更新界面数据 通过m++实现加速度方式的不断加速绘制弧形
						 * i代表要绘制扇形的角度大小，默认下360度
						 */

						circleHandler.sendEmptyMessage(1);
					} catch (InterruptedException e) {

						e.printStackTrace();
					}

				}
			}
		});
	}

}
